﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;
namespace Data
{
    public class ThamSoRepository
    {



		//////////////////////////////////////////////////////////

		                                                          

		//            Enter code your here                      //




        //lay so luong khach toi da trong moi phong

        int iMax = 99999999;
        public int SokhachToiDa()
        {
            ThamSoDTO ts = this.LayTheoMa(1);
            if (ts == null)
                return iMax;

            if (ts.SuDung == true)
            {
                return int.Parse(ts.GiaTri.ToString());
            }
            return iMax;

        }
        public int SoLoaiPhongToiDa()
        {
            ThamSoDTO ts = this.LayTheoMa(ThamSoDTO.MaThamSoSoLoaiPhongToiDa);

            if (ts.SuDung == true)
            {
                int val = int.Parse(ts.GiaTri.ToString());
                return val;
            }
            return iMax;
        }

        //tinh ti le phu thu so luong khach
        public double PhuThuSoLuongKhach(List<KhachHangDTO> lst)
        {
            int iSoKhachToiDa = this.SokhachToiDa();

            if (iSoKhachToiDa <= 0 || iSoKhachToiDa > lst.Count)
            {
                return 0;
            }

            ThamSoDTO ts = this.LayTheoMa(ThamSoDTO.MaThamSoPhuThuSoLuongKhach);
            if (ts == null)
                return 0;

            if (ts.SuDung == true)
            {
                return ts.GiaTri;
            }
            return 0;
        }

        //tinh phu thu khach nuoc ngoai
        public double PhuThuKhachNuocNgoai(List<KhachHangDTO> lst)
        {

            KhachHangRepository repo = new KhachHangRepository();
            if (repo.KiemTraKhachNuocNgoai(lst) == false)
                return 1;


            ThamSoDTO ts = this.LayTheoMa(ThamSoDTO.MaThamSoPhuThuKhachNuocNgoai);
            if (ts == null || ts.SuDung == false)
                return 1;

            if (ts.SuDung == true)
            {
                return ts.GiaTri;
            }
            return 1;
        }




		//////////////////////////////////////////////////////////

        public static ThamSoDTO CreateThamSoDTOFromThamSoEF(ThamSo objEf) // EF -> DTO
        {
            ThamSoDTO objDTO = new ThamSoDTO();
            Mapper.CreateMap<ThamSo, ThamSoDTO>();
            objDTO = Mapper.Map<ThamSo, ThamSoDTO>(objEf);
            return objDTO;
        }

        public static ThamSo CreateThamSoEFFromThamSoDTO(ThamSoDTO objDTO)    //DTO -> EF
        {
            ThamSo objEF = new ThamSo();
            Mapper.CreateMap<ThamSoDTO, ThamSo>();
            objEF = Mapper.Map<ThamSoDTO, ThamSo>(objDTO);
            return objEF;
        }

       public static int Count()  //Dem so luong cac bo du lieu trong bang
        {
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            int qr = (from x in Context.ThamSoList
                   select x.MaThamSo).Count();
            return qr;
        }

        public int GetIdentity() // tra ve gia tri ma moi them vo( gia tri Ma lon nhat trong Bang).
        {
            try
            {
                 QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
                  var query = from x in Context.ThamSoList
                        orderby x.MaThamSo descending
                        select x.MaThamSo;
                  var Ma = query.First();
                  return int.Parse(Ma.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public bool Them(ThamSoDTO objDTO)
        {
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                ThamSo objEF = CreateThamSoEFFromThamSoDTO(objDTO);
                context.AddToThamSoList(objEF);
                context.SaveChanges();
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

        public void CapNhat(ThamSoDTO objDTO)
        {
            try
            {
                ThamSo objEF = CreateThamSoEFFromThamSoDTO(objDTO);
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                context.ThamSoList.ApplyCurrentValues(objEF);
                context.SaveChanges();
            }
            catch (System.Exception )
            {            	
            }
        }

        public List<ThamSoDTO> LayDanhSach(int Start, int Record)//lay Record bo du lieu tu vi tri Start
        { 
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            var query = (from x in Context.ThamSoList
                        orderby x.MaThamSo
                        select x).Skip(Start - 1).Take(Record);
            List<ThamSoDTO> lst = new List<ThamSoDTO>();
            foreach (var obj in query)
            {
                ThamSoDTO lp = ThamSoRepository.CreateThamSoDTOFromThamSoEF(obj);
                lst.Add(lp);
            }
            return lst;
        }

        public List<ThamSoDTO> LayDanhSach()//lay tat ca danh sach
        {
            List<ThamSoDTO> lst = new List<ThamSoDTO>();
            QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
            var query = from ThamSo in context.ThamSoList
                        select ThamSo;
            foreach (var objEf in query)
            {
                ThamSoDTO objDTO = CreateThamSoDTOFromThamSoEF(objEf);
                lst.Add(objDTO);
            }
            return lst;
        }

        public ThamSoDTO LayTheoMa(int MaThamSo)
        {
            ThamSoDTO objDTO = new ThamSoDTO();
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                var query = from ThamSo in context.ThamSoList
                            where ThamSo.MaThamSo == MaThamSo
                            select ThamSo;
                ThamSo objEF = query.First();
                objDTO = CreateThamSoDTOFromThamSoEF(objEF);
            }
            catch (System.Exception )
            {
            }
            return objDTO;
        }
        public bool Xoa(int MaThamSo, bool XoaKhoaNgoai=false)
        {
            try
            {
                if( XoaKhoaNgoai == true)
                {
                
                }
                QuanLyKhachSanEntities context = new QuanLyKhachSanEntities();
                var query = from x in context.ThamSoList
                            where x.MaThamSo == MaThamSo 
                            select x;
                if(query.First()!= null)
                {
                    context.DeleteObject(query.First());
                    context.SaveChanges();
                }
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

////////////////////////////////////////////////////////////////

    }
}

